package com.ethan.cj.ssm.service.impl;

import com.ethan.cj.ssm.dao.LoginDao;
import com.ethan.cj.ssm.entity.People;
import com.ethan.cj.ssm.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    /**
     * 通过用户名获取用户信息
     *
     * @param peopleJson 用户名密码Json字符串
     * @param request  HttpServlet请求
     * @return 返回JSON结果信息
     */
    @Override
    public Map<Object, Object> getPeopleByPname(Map<String, String> peopleJson, HttpServletRequest request) {
        Map<Object, Object> result = new HashMap<>();
        String username = peopleJson.get("username");
        String password = peopleJson.get("password");
        People people = loginDao.getPeopleByPname(username);
        if (people != null) {
            if (people.getPassword().equals(password)) {
                result.put("code", 1);
                result.put("msg", "登录成功！");
                request.getSession().setAttribute("peopleinfo", people);
            } else {
                result.put("code", 0);
                result.put("msg", "密码错误！");
            }
        } else {
            result.put("code", 0);
            result.put("msg", "用户名不存在！请先注册！");
        }
        return result;
    }
}
